import Common from '@/utils/common.js';
import {
  LOGIN,
  WX_BIND_PHONE,
  GET_ACCOUNT_INFO,
  USER_RECENT_COMING,
  //mutations
  UPDATE_USER_RELEASE_COUNT,
  GET_MEMBER_EXPAND_INFO,
} from './actions';

const prefixUrl = () => uni.$http.userPrefixUrl;
export default {
  state: {
    token: '',
    umsMember: {},
    userInfo: {},
    appName: '',
    version: '',
  },
  getter: {

  },
  mutations: {
    // 设置token
    setToken(state, { token = '' }) {
      state.token = token;
      Common.setStorage('login@token', token);
    },
    // 绑定用户信息
    bindUserInfo(state, { token = '', umsMember = {}, userInfo = {} }) {
      if (token) {
        state.token = token;
      }
      if (umsMember && Object.keys(umsMember).length) {
        // console.log(1111111111, userInfo, umsMember);
        const { id = '' } = state.umsMember;
        // if (!id && umsMember.id) {
        //   uni.$socket.initSocket(umsMember.id);
        // }
        state.umsMember = umsMember;
      }
      if (userInfo && Object.keys(userInfo).length) {
        state.userInfo = userInfo;
      }
    },
    // 更新发布数和积分
		updateReleInter(state, { releCnt = '', integration = '' }) {
      // console.log(33333333333333, releCnt, integration);
      state.umsMember = {
        ...state.umsMember,
        releCnt,
        integration,
      }
    },
    // 编辑用户信息
		updateMember(state, { nickname = '', icon = '' }) {
      // console.log(33333333333333, nickname, icon);
      state.umsMember = {
        ...state.umsMember,
        nickname,
        icon,
      }
    },
    // 获取小程序信息
    getAccountInfo(state, { appName, version }) {
      state.appName = appName;
      state.version = version;
    },
    [UPDATE_USER_RELEASE_COUNT] (state, { isAdd }) {
      let { releCnt } = state.umsMember;
      releCnt = releCnt || 0;
      releCnt = isAdd ? Number(releCnt) + 1 : Number(releCnt) - 1;
      if (releCnt < 0) releCnt = 0;
      state.umsMember = {
        ...state.umsMember,
        releCnt,
      }
    },
  },
  actions: {
    async [LOGIN] ({ commit }) {
      const code = await Common.wxLogin();
      const accountInfo = wx.getAccountInfoSync();
      const params = {
        code,
        appId: accountInfo.miniProgram.appId, // 小程序 appId
      }
      const res = await uni.$http.post(prefixUrl() + '/sso/wxLogin', params);
      // console.log('wxLogin', params, res);
      if (res && res.data) {
        const { token = '', tokenHead = '', umsMember = {} } = res.data;
        const normalToken = `${tokenHead}${token}`
        Common.setStorage('login@token', normalToken);
        commit('bindUserInfo', { token: normalToken, umsMember });
      }
    },
    async [WX_BIND_PHONE] ({ commit }, data) {
      try {
        const code = await Common.wxLogin();
        const params = {
          code,
          ...data,
        }
        const res = await uni.$http.post(prefixUrl() + '/sso/wxBindPhone', params);

        if (res && res.code === 200) {
          const { data = {} } = res;
          const {
            token = '',
            tokenHead = '',
            umsMember,
            userInfo,
          } = data;
          const normalToken = `${tokenHead}${token}`
          Common.setStorage('login@token', normalToken);
          Common.setStorage('umsMember', umsMember);
          Common.setStorage('userInfo', userInfo);
          commit('bindUserInfo', { token: normalToken, umsMember, userInfo });
          return true;
        }
      } catch (error) {
        return false;
      }
    },

    async [GET_ACCOUNT_INFO] ({ commit }) {
      const res =  uni.getAccountInfoSync()
      if (res) {
        const { miniProgram = {}, appName = '' } = res;
        const { version = '' } = miniProgram;
        commit('getAccountInfo', { appName, version });
      }
    },

    async [GET_MEMBER_EXPAND_INFO] ({ commit }) {
      try {
        const res = await uni.$http.post(prefixUrl() + '/sso/getMemberExpandInfo');

        if (res && res.code === 200) {
          const { data = {} } = res;
          commit('updateReleInter', data);
        }
      } catch (error) {
        commit('updateReleInter', {});
      }
    },

    async [USER_RECENT_COMING] () {
      try {
        const res = await uni.$http.get(prefixUrl() + '/sso/call');
        if (res && res.code === 200) {
        }
      } catch (error) {
        console.error(error);
      }
    }
  }
}